//
//  SettingLayer.cpp
//  Space shooting
//
//  Created by TungNguyen on 10/02/13.
//
//

#include "SettingLayer.h"
#include "Constants.h"
#include "StateManager.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;

SettingLayer::SettingLayer() {
    
    init();
}

SettingLayer::~SettingLayer() {
    removeAllChildrenWithCleanup(true);
}

bool SettingLayer::init() {
    
    if(!CCLayer::init()) {
        
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    CCMenu *menu = CCMenu::create();

    // Background
    CCSprite *background = CCSprite::create("home_bg.png");
    background->setScaleX(size.width/background->getTexture()->getContentSize().width);
    background->setScaleY(size.height/background->getTexture()->getContentSize().height);
    
    background->setPosition(ccp(size.width/2, size.height/2));
    
    addChild(background);
    
    /* ------------------------------------------------------------------------------------------------------------------------ */
    
    // Game Setting title
    CCLabelTTF *settingLabel = CCLabelTTF::create("Settings", "Spac3 neon.ttf", 30);    
    settingLabel->setPosition(ccp(size.width/2, size.height - settingLabel->getTexture()->getContentSize().height));
    settingLabel->setColor(ccc3(255, 109, 1));
    addChild(settingLabel);
    
    /* ------------------------------------------------------------------------------------------------------------------------ */
    
    // Sprite Toggle Background
    CCMenuItemSprite* sprite1 = CCMenuItemSprite::create(CCSprite::create("unchecked.png"), CCSprite::create("checked.png"));
    sprite1->setScale(0.7);
    CCMenuItemSprite* sprite2 = CCMenuItemSprite::create(CCSprite::create("checked.png"), CCSprite::create("unchecked.png"));
    sprite2->setScale(0.7);
    
    // Sound menu item
    CCLabelTTF *soundLabel = CCLabelTTF::create("Sound", "Fonts/Digital tech.otf", 25);
    CCMenuItemLabel *soundItem = CCMenuItemLabel::create(soundLabel, this, NULL);
    soundItem->setPosition(ccp(0, size.height / 2));
    soundItem->setAnchorPoint(ccp(1, 0));
    menu->addChild(soundItem, 4);
    
    // Add sound toggle button
    CCMenuItemToggle* soundToggle = CCMenuItemToggle::createWithTarget(this, menu_selector(SettingLayer::soundToggleClicked) , sprite1, sprite2, NULL);
    soundToggle->setPosition(ccp(60, size.height / 2 - 5));
    soundToggle->setAnchorPoint(ccp(1, 0));
    soundToggle->setTag(SETTING_MENU_SOUND);
    menu->addChild(soundToggle);
    
    /* ------------------------------------------------------------------------------------------------------------------------ */

    // Sprite Toggle Background
    CCMenuItemSprite* sprite3 = CCMenuItemSprite::create(CCSprite::create("unchecked.png"), CCSprite::create("checked.png"));
    sprite3->setScale(0.7);
    CCMenuItemSprite* sprite4 = CCMenuItemSprite::create(CCSprite::create("checked.png"), CCSprite::create("unchecked.png"));
    sprite4->setScale(0.7);

    // Vibrate menu item
    CCLabelTTF *vibrateLabel = CCLabelTTF::create("Vibrate", "Fonts/Digital tech.otf", 25);
    CCMenuItemLabel *vibrateItem = CCMenuItemLabel::create(vibrateLabel, this, NULL);
    //    soundItem->setTag(HOME_MENU_EXIT);
    vibrateItem->setPosition(ccp(0, size.height / 2 - 50));
    vibrateItem->setAnchorPoint(ccp(1, 0));
    menu->addChild(vibrateItem, 3);
    
    // Add Vibrate toggle button
    CCMenuItemToggle* vibrateToggle = CCMenuItemToggle::createWithTarget(this, menu_selector(SettingLayer::vibrateToggleClicked) , sprite3, sprite4, NULL);
    vibrateToggle->setPosition(ccp(60, size.height / 2 - 55));
    vibrateToggle->setAnchorPoint(ccp(1, 0));
    vibrateToggle->setTag(SETTING_MENU_VIBRATE);
    menu->addChild(vibrateToggle);

    /* ------------------------------------------------------------------------------------------------------------------------ */
    
    // Sprite Toggle Background
    CCMenuItemSprite* sprite5 = CCMenuItemSprite::create(CCSprite::create("unchecked.png"), CCSprite::create("checked.png"));
    sprite5->setScale(0.7);
    CCMenuItemSprite* sprite6 = CCMenuItemSprite::create(CCSprite::create("checked.png"), CCSprite::create("unchecked.png"));
    sprite6->setScale(0.7);
    
    // Auto Fire menu item
    CCLabelTTF *autofireLabel = CCLabelTTF::create("Auto Fire", "Fonts/Digital tech.otf", 25);
    CCMenuItemLabel *autoFireItem = CCMenuItemLabel::create(autofireLabel, this, NULL);
    //    soundItem->setTag(HOME_MENU_EXIT);
    autoFireItem->setPosition(ccp(0, size.height / 2 - 100));
    autoFireItem->setAnchorPoint(ccp(1, 0));
    menu->addChild(autoFireItem, 2);
    
    // Add Auto Fire toggle button
    CCMenuItemToggle* autoFireToggle = CCMenuItemToggle::createWithTarget(this, menu_selector(SettingLayer::autoFireToggleClicked) , sprite5, sprite6, NULL);
    autoFireToggle->setPosition(ccp(60, size.height / 2 - 105));
    autoFireToggle->setAnchorPoint(ccp(1, 0));
    autoFireToggle->setTag(SETTING_MENU_AUTOFIRE);
    menu->addChild(autoFireToggle);
    
    /* ------------------------------------------------------------------------------------------------------------------------ */
    
    // Exit menu item
    CCLabelTTF *exitLable = CCLabelTTF::create("Back to Main menu", "Fonts/Digital tech.otf", 25);
    CCMenuItemLabel *exitItem = CCMenuItemLabel::create(exitLable, this, menu_selector(SettingLayer::exitScreen));
    exitItem->setTag(SETTING_MENU_EXIT);
    exitItem->setPosition(ccp(10, size.height / 2 - 150));
    menu->addChild(exitItem, 1);
    
    /* ------------------------------------------------------------------------------------------------------------------------ */
    
    menu->setPosition(ccp(size.width/2, 50));
    
    addChild(menu);
    
    /* ------------------------------------------------------------------------------------------------------------------------ */
    // Init Setting value
    CCUserDefault::sharedUserDefault()->getBoolForKey(SOUND_SETTING_KEY)?soundToggle->setSelectedIndex(1):soundToggle->setSelectedIndex(0);
    CCUserDefault::sharedUserDefault()->getBoolForKey(VIBRATE_SETTING_KEY)?vibrateToggle->setSelectedIndex(1):vibrateToggle->setSelectedIndex(0);
    CCUserDefault::sharedUserDefault()->getBoolForKey(AUTOFIRE_SETTING_KEY)?autoFireToggle->setSelectedIndex(1):autoFireToggle->setSelectedIndex(0);
    
    return true;
}

void SettingLayer::soundToggleClicked(CCObject *sender) {
    CCMenuItemToggle* toggleButton = (CCMenuItemToggle *)sender;

    if(toggleButton->getSelectedIndex() == 1) { // On
        CCLOG("SOUND ON");
        CCUserDefault::sharedUserDefault()->setBoolForKey(SOUND_SETTING_KEY, true);
    } else { // Off
         CCLOG("SOUND OFF");
        CCUserDefault::sharedUserDefault()->setBoolForKey(SOUND_SETTING_KEY, false);
    }
}

void SettingLayer::vibrateToggleClicked(CCObject *sender) {
    CCMenuItemToggle* toggleButton = (CCMenuItemToggle *)sender;
    if(toggleButton->getSelectedIndex() == 1) { // ON
        CCLOG("VIBRATE ON");
        CCUserDefault::sharedUserDefault()->setBoolForKey(VIBRATE_SETTING_KEY, true);
    } else { // Off
        CCLOG("VIBRATE OFF");
        CCUserDefault::sharedUserDefault()->setBoolForKey(VIBRATE_SETTING_KEY, false);
    }
}

void SettingLayer::autoFireToggleClicked(CCObject *sender) {
    CCMenuItemToggle* toggleButton = (CCMenuItemToggle *)sender;
    if(toggleButton->getSelectedIndex() == 1) { // On
        CCLOG("AUTO FIRE ON");
        CCUserDefault::sharedUserDefault()->setBoolForKey(AUTOFIRE_SETTING_KEY, true);
    } else { // Off
        CCLOG("AUTO FIRE OFF");
        CCUserDefault::sharedUserDefault()->setBoolForKey(AUTOFIRE_SETTING_KEY, false);
    }
}

void SettingLayer::exitScreen(CCObject *sender) {
     StateManager::getInstance()->switchScene(GAME_STATE_HOME);
}